/*
 * 文件名：DbCommon.java
 * 版权：Copyright by will_awoke
 * 描述：
 * 修改人：lyh
 * 修改时间：2013-10-15
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.qbgs.util;


import java.sql.Types;

import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;


/**
 * DB常用操作类
 * @author lyh
 * @version 2013-10-15
 * @see DbCommon
 * @since
 */
public class DbCommon
{

    /**
     * 日志
     */
    private static final Logger log = Logger.getLogger(DbCommon.class);

    /**
     * JdbcTemplate
     */
    private static JdbcTemplate jdbcTemplate;

    static
    {
        jdbcTemplate = (JdbcTemplate)ContextBeanUtils.getBean("jdbcTemplate");
    }

    /**
     * 根据appId,appSecret查询serviceId
     * @param appId
     * @param appSecret
     * @return 存在符合条件的记录则返回对应的serviceId，否则返回-1
     * @see
     */
    public static Integer getServiceId(String appId, String appSecret)
    {
        int serviceid = -1;

        String sql = "select serviceid from thirdservice where appid=? and appsecret=?";
        Object[] params = new Object[] {appId, appSecret};
        int[] types = new int[] {Types.VARCHAR, Types.VARCHAR};
        try
        {
            serviceid = jdbcTemplate.queryForInt(sql, params, types);
        }
        catch (EmptyResultDataAccessException e)
        {
            log.warn("query serviceId by appId=" + appId + ", appSecret=" + appSecret
                     + ", incorrect result size: expected 1, actual 0");
            return -1;
        }
        return serviceid;
    }

}
